const crypto = require("crypto");

// 解析手机号
function decryptData(encryptedData, sessionKey, iv) {
  try {
    // 对输入的 base64 编码数据进行解码
    const encryptedBuffer = Buffer.from(encryptedData, "base64");
    const sessionKeyBuffer = Buffer.from(sessionKey, "base64");
    const ivBuffer = Buffer.from(iv, "base64");

    // 创建 AES 解密器
    const decipher = crypto.createDecipheriv(
      "aes-128-cbc",
      sessionKeyBuffer,
      ivBuffer
    );

    // 解密数据
    let decrypted = decipher.update(encryptedBuffer, null, "utf8");
    decrypted += decipher.final("utf8");

    return decrypted;
  } catch (error) {
    console.error("解密失败:", error);
    return null;
  }
}

module.exports = decryptData;
